package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.dto.base.PageDTO;
import com.kaizeli.website.pojo.entity.ProjectContractDO;
import com.kaizeli.website.pojo.vo.ProjectContractVO;
import com.kaizeli.website.query.ProjectContractQuery;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.ProjectContractService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目合同管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/project-contracts")
@RequiredArgsConstructor
@Tag(name = "项目合同管理", description = "项目合同信息管理相关接口")
@SecurityRequirement(name = "Bearer Authentication")
public class ProjectContractController {
    
    private final ProjectContractService projectContractService;
    
    @Operation(summary = "分页查询项目合同列表", description = "支持按项目ID、合同编号、合同金额等条件分页查询项目合同信息")
    @GetMapping("/page")
    public Result<PageDTO<ProjectContractVO>> pageQuery(ProjectContractQuery query) {
        log.info("分页查询项目合同列表，查询条件：{}", query);
        PageDTO<ProjectContractVO> result = projectContractService.pageQuery(query);
        return Result.success(result);
    }
    
    @Operation(summary = "根据ID查询项目合同详情", description = "根据项目合同ID查询详细信息")
    @GetMapping("/{id}")
    public Result<ProjectContractVO> getById(
            @Parameter(description = "项目合同ID", required = true, example = "1")
            @PathVariable Long id) {
        log.info("根据ID查询项目合同详情，ID：{}", id);
        ProjectContractVO result = projectContractService.getById(id);
        return Result.success(result);
    }
    
    @Operation(summary = "新增项目合同", description = "创建新的项目合同信息")
    @PostMapping
    public Result<Long> save(
            @Parameter(description = "项目合同信息", required = true)
            @RequestBody ProjectContractDO projectContractDO) {
        log.info("新增项目合同，项目合同信息：{}", projectContractDO);
        // 清除ID，确保使用数据库自增ID
        projectContractDO.setId(null);
        Long result = projectContractService.save(projectContractDO);
        return Result.success(result);
    }
    
    @Operation(summary = "更新项目合同信息", description = "根据ID更新项目合同信息")
    @PutMapping("/{id}")
    public Result<Boolean> update(
            @Parameter(description = "项目合同ID", required = true, example = "1")
            @PathVariable Long id,
            @Parameter(description = "更新的项目合同信息", required = true)
            @RequestBody ProjectContractDO projectContractDO) {
        log.info("更新项目合同信息，ID：{}，更新信息：{}", id, projectContractDO);
        projectContractDO.setId(id);
        Boolean result = projectContractService.update(projectContractDO);
        return Result.success(result);
    }
    
    @Operation(summary = "根据ID删除项目合同", description = "根据项目合同ID删除信息")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(
            @Parameter(description = "项目合同ID", required = true, example = "1")
            @PathVariable Long id) {
        log.info("根据ID删除项目合同，ID：{}", id);
        Boolean result = projectContractService.deleteById(id);
        return Result.success(result);
    }
    
    @Operation(summary = "根据项目ID查询项目合同列表", description = "根据项目ID查询该项目的所有合同")
    @GetMapping("/project/{projectId}")
    public Result<List<ProjectContractVO>> listByProjectId(
            @Parameter(description = "项目ID", required = true, example = "1")
            @PathVariable Long projectId) {
        log.info("根据项目ID查询项目合同列表，项目ID：{}", projectId);
        List<ProjectContractVO> result = projectContractService.listByProjectId(projectId);
        return Result.success(result);
    }
    
    @Operation(summary = "查询所有项目合同列表", description = "查询系统中所有项目合同信息")
    @GetMapping("/all")
    public Result<List<ProjectContractVO>> listAll() {
        log.info("查询所有项目合同列表");
        List<ProjectContractVO> result = projectContractService.listAll();
        return Result.success(result);
    }
}

